package com.cqc.dialogfragment01;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by cui on 2018/1/27.
 * dialogFragment
 */

public class DialogFragment04 extends DialogFragment {


    private String title;//title
    private String cancel;//取消按钮描述
    private ArrayList<String> items;//list列表数据

    public static DialogFragment04 getInstance(String title, String cancel, ArrayList<String> items) {

        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("cancel", cancel);
        bundle.putStringArrayList("items", items);

        DialogFragment04 frag = new DialogFragment04();
        frag.setArguments(bundle);
        return frag;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        title = bundle.getString("title", "标题");
        cancel = bundle.getString("cancel", "取消");
        items = bundle.getStringArrayList("items");
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.gravity = Gravity.BOTTOM;

        window.setAttributes(params);
        window.setBackgroundDrawableResource(android.R.color.transparent);//注视掉的话，dialog宽度不会充满屏幕
        //目的：退出时使用动画
//        window.getDecorView().setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                if (event.getAction() == MotionEvent.ACTION_DOWN) {
//                    dismiss();
//                }
//                return true;
//            }
//        });
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return inflater.inflate(R.layout.dialog_frag, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().setCanceledOnTouchOutside(false);
        slideToUp(view);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        TextView tvCancel =  view.findViewById(R.id.tv_cancel);
        ListView listView = view.findViewById(R.id.listView);

        tvTitle.setText(title);
        tvCancel.setText(cancel);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        ArrayAdapter<String> adapter = new ArrayAdapter<>(view.getContext(), R.layout.item_dailog, items);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(clickListener!=null){
                    clickListener.onClick(parent,view,position,id);
                    dismiss();
                }
            }
        });
    }

    public interface OnClickListener{
       void  onClick(AdapterView<?> parent, View view, int position, long id);
    }
    private OnClickListener clickListener;
    public void setOnClickListener(OnClickListener clickListener){
        this.clickListener = clickListener;
    }


    public void slideToUp(View view) {
        TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0f);
        animation.setFillAfter(true);
        animation.setDuration(400);
        view.startAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    @Override
    public void dismiss() {
        slideToDown(getView());
    }


    public void slideToDown(View view) {
        TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1);
        animation.setFillAfter(true);
        animation.setDuration(400);
        view.startAnimation(animation);

        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                DialogFragment04.super.dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }
}
